#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 10 jul 2011

@author: Joakim Lindskog

Copyright (c) 2011 Joakim Lindskog

This file is part of MyGarden.

    MyGarden is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MyGarden is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MyGarden.  If not, see <http://www.gnu.org/licenses/>.
'''
import pyglet, tool, util, events
from cocos.director import director

class Grass(tool.Tool):
    '''
    Grass as fertiliser for the beds
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.idle_image = util.load_image("basket_grass.png", fix=False)
        super(Grass, self).__init__(self.idle_image)
        self.use_anim = util.setup_animation('fertiliser_grass_tiled.png', 5, 6, 0.04)
        self.image_anchor = 15,15
        
        # Target bed
        self.target = None
        
        # Use sound
        self.sound = util.load_effect('grass.ogg')
        
    def use(self, bed, x, y, button, modifiers):
        self.target = bed
        self.image = self.use_anim
        pyglet.clock.schedule_interval(self.fertilise, 1)
        # Dispatch event
        events.EventBus().dispatch_event(events.UseToolEvent(self, x, y, name="grass_fertiliser"))
        
    def cancel(self):
        self.image = self.idle_image
        self.target = None
        pyglet.clock.unschedule(self.fertilise)
        
    def fertilise(self, dt):
        '''
        Starts fertilising
        
        @param dt: time elapsed since last update, in seconds
        @type dt: float
        '''
        self.target.grass += 7.0
        self.sound.play()
        #print "grass: "+str(self.target.grass)+" nutr:"+str(self.target.nutrients)
        
    def get_cursor(self):
        return director.interpreter_locals["director"].window.get_system_mouse_cursor(pyglet.window.BaseWindow.CURSOR_CROSSHAIR)
    
    def get_mouse_info_image(self):
        return self